package com.mdh.runerrands.controller;

import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.Category;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.ICategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/controller/category")
public class CategoryController {
    @Resource
    private ICategoryService categoryService;
    @ApiOperation("批量删除分类信息")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = categoryService.batchDelete(ids);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }

    }
    @ApiOperation("通过ID删除分类信息")
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = categoryService.deleteById(id);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation("分页查找分类信息")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Result query(int page,int pageSize,String name,String num){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<Category> query = categoryService.query(page, pageSize, name, num);
            QueryPage queryPage = new QueryPage();
            queryPage.setList(query);
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            int i = categoryService.selectCount(name, num);
            queryPage.setTotal(i);
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation("保存或者更新分类信息")
    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Result saveOrUpdate(Category category){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            if (category.getId() != 0){
                boolean update = categoryService.update(category);
                result.setMessage("success");
                result.setData(update);
                return result;
            }else {
                boolean insert = categoryService.insert(category);
                result.setData(insert);
                result.setMessage("success");
                return result;
            }
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation("查询所有栏目信息")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<Category> all = categoryService.findAll();
            result.setData(all);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setStatus("500");
            result.setData(e.getMessage());
            result.setMessage("Exception");
            return result;
        }

    }
}
